/*
 * Copyright (c) 2023 fred
 * Licensed under the Apache License,Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
import { Connection, ConnectionKeyVal } from '../Connection'
import { Tag } from '../parser/Tag'
import { Elements } from '../select/Elements'
import { Attributes } from './Attributes'
import { Element } from './Element'
import { Node } from './Node'
import List from '@ohos.util.List'

/**
 * A HTML Form Element provides ready access to the form fields/controls that are associated with it. It also allows a
 * form to easily be submitted.
 */
export class FormElement extends Element {
  private _elements = new Elements()

  constructor(tag: Tag, baseUri: string, attributes: Attributes) {
    super(tag, baseUri, attributes)
  }

  public getElements(): Elements {
    return this._elements
  }

  /**
   * Add a form control element to this form.
   * @param element form control to add
   * @return this form element, for chaining
   */
  public addElement(element: Element): FormElement {
    this._elements.add(element)
    return this
  }

  removeChild(out: Node) {
    super.removeChild(out)
    let index = this._elements.indexOf(out as Element)
    if (index !== -1) {
      this._elements.splice(index,1)
    }
  }

  /**
   Prepare to submit this form. A Connection object is created with the request set up from the form values. This
   Connection will inherit the settings and the cookies (etc) of the connection/session used to request this Document
   (if any), as available in {@link Document#connection()}
   <p>You can then set up other options (like user-agent, timeout, cookies), then execute it.</p>

   @return a connection prepared from the values of this form, in the same session as the one used to request it
   @throws IllegalArgumentException if the form's absolute action URL cannot be determined. Make sure you pass the
   document's base URI when parsing.
   */
  public submit(): Connection | null {
    //todo
    return null
  }

  /**
   * Get the data that this form submits. The returned list is a copy of the data, and changes to the contents of the
   * list will not be reflected in the DOM.
   * @return a list of key vals
   */
  public formData(): List<ConnectionKeyVal> {
    //todo
    return new List()
  }

  public clone(): FormElement {
    return super.clone() as FormElement
  }
}
